package com.woniuxy.member.service.util;

import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.security.Keys;

import java.security.Key;
import java.util.Date;
import java.util.Map;

public class JwtUtil {
    //自定义的秘钥
    //HS256：即字节长度>=32
    static String sect ="wokeguguayibeizibutmycpbixushijidhfgjhttydafehhh";
    //生成token
    public static String createToken(Map<String, Object> body, Integer mil) {
        Key key = Keys.hmacShaKeyFor(sect.getBytes());
        return Jwts.builder()
                .setClaims(body)
                .setExpiration(new Date(System.currentTimeMillis() + mil * 60 * 1000))
                .signWith(key)
                .compact();
    }
    //解析token
    public static Claims parseToken(String token) {
        Key key = Keys.hmacShaKeyFor(sect.getBytes());
        return Jwts.parserBuilder().setSigningKey(key)
                .build().parseClaimsJws(token).getBody();
    }


}
